perf: optimize container lookup with early exit#1431
Open
Bitshifter-9 wants to merge 1 commit intotesting-library:mainfrom
Open
perf: optimize container lookup with early exit#1431Bitshifter-9 wants to merge 1 commit intotesting-library:mainfrom
Bitshifter-9 wants to merge 1 commit intotesting-library:mainfrom
Conversation
Replace forEach with find() in render() function's container lookup logic to enable early exit when a matching container is found. The previous implementation used forEach which continues iterating through all entries even after finding the match. This change uses find() which stops immediately upon finding a match, improving performance especially when multiple containers are mounted. - Improves performance with early exit optimization - More idiomatic JavaScript pattern - Maintains identical behavior and test coverage - All 247 tests pass successfully
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 9add965:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
eplaced an inefficient forEach loop with the find() method in the
render()
function's container lookup logic, enabling early exit when a matching container is found.
Type of Change
Bug fix (performance optimization)
New feature
Breaking change
Documentation update
The Problem
In
src/pure.js
lines 286-293, when reusing an existing container, the code used forEach to search through mountedRootEntries:
javascript
mountedRootEntries.forEach(rootEntry => {
if (rootEntry.container === container) {
root = rootEntry.root
}
})
The forEach method continues iterating through all entries even after finding the matching container. This is inefficient because:
It performs unnecessary iterations after the match is found
Performance degrades as more containers are mounted
The code doesn't communicate the intent to find a single entry
The Solution
Replaced forEach with the find() method which stops iteration immediately upon finding a match:
#1430
javascript
const rootEntry = mountedRootEntries.find(
rootEntry => rootEntry.container === container,
)
if (rootEntry) {
root = rootEntry.root
}
This change:
⚡ Stops searching immediately when a match is found (early exit optimization)
📈 Improves performance, especially with multiple mounted containers
🎯 Makes the code intent clearer - we're looking for one specific entry
✅ Uses a more idiomatic JavaScript pattern